module.exports = class extends baseController {
    constructor() {
        super();
        this.articleLogic = this.loadLogic('cms/article');
        this.artLabelLogic = this.loadLogic('cms/article-label');
        this.router.post('/getItems', this.getItems.bind(this));
        this.router.post('/getItem', this.getItem.bind(this));
        this.router.post('/edit', this.edit.bind(this));
        this.router.post('/del', this.del.bind(this));
        return this.router;
    }

    static init() {
        return new this();
    }

    async getItems(req, res) {
        let Result = {};
        try {
            let FindFilterModel = {};
            let CurPage = req.body.CurPage ? Number(req.body.CurPage) : 1;
            let PageSize = req.body.PageSize ? Number(req.body.PageSize) : 20;
            let Sort = req.body.Sort ? req.body.Sort : "Art_CreateTime|desc";
            FindFilterModel.Query = req.body.Query;
            FindFilterModel.Art_Title = req.body.Art_Title;
            FindFilterModel.AR_LabelCode = req.body.AR_LabelCode;
            FindFilterModel.Art_IsRecommend = req.body.Art_IsRecommend;
            FindFilterModel.Art_IsPreShowImg = req.body.Art_IsPreShowImg;
            FindFilterModel.Art_IsHide = req.body.Art_IsHide;
            FindFilterModel.Art_CreateTime = req.body.Art_CreateTime;

            let Ret = await this.articleLogic.getItems(FindFilterModel, CurPage, PageSize, Sort, req.headers.moduleid, req.user);
            let ArtLabelsItems = await this.artLabelLogic.getItemsByDegree(req.headers.moduleid,req.user);
            Ret.ArtLabels=await this.artLabelLogic.GetTreeData(0,ArtLabelsItems);
            //Ret.ArtLabels = await this.func.iframeDict.GetTreeData('ArticleLabel');
            Result = this.Success(1, Ret);
        } catch (err) {
            Result = this.Error("查询失败", err);
        }
        res.json(Result);
    }

    async getItem(req, res) {
        let Ret = {};
        let Result = {};
        try {
            let id = req.body.id || '';
            if (id !== '') {
                Ret.Model = await this.articleLogic.getItem(id);
            } else {
                Ret.Model = {
                    Art_title:'',
                    Art_Content:'',
                    Art_IsPreShowImg: 0,
                    Art_ReadCount: 0,
                    Art_ViewStyle: 6,
                    Art_IsHide: 0,
                    Art_IsRecommend: 0
                };
            }

            let ArtLabelsItems = await this.artLabelLogic.getItemsByDegree(req.headers.moduleid,req.user);
            Ret.ArtLabels=await this.artLabelLogic.GetTreeData(0,ArtLabelsItems);
            Result = this.Success(1, Ret);
        } catch (err) {
            Result = this.Success(err);
        }
        res.json(Result);
    }

    async edit(req, res) {
        let Result = {};
        try {
            let model = req.body;
            let ret = await this.articleLogic.insertOrUpdate(model,req.user);
            if (ret) {
                Result = this.Success(1);
            } else {
                Result = this.Error("编辑失败");
            }
        } catch (err) {
            Result = this.Error("编辑失败",err);
        }
        res.json(Result);
    }

    async del(req, res) {
        let Result = {};
        try {
            let keys = req.body.keys;
            await this.articleLogic.del(keys);
            Result = this.Success(1);
        } catch (err) {
            Result = this.err("操作失败", err);
        }
        res.json(Result);
    }
};




